/**
 * Created by zhangxiaoyu on 16/12/08.
 */
angular.module('hyt.outerService', ['ngResource'])

  .factory('TransferList', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.getTransferBills,
      transferData={},pageSize=10;

    var resource = $resource(ApiUrl+':transferSearch',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getTransferData : function(transferSearch){
        var hasNextPage = true;
        resource.query({
          transferSearch:JSON.stringify({
            pageSize:'10',
            pageNo:'1',
            parameter:transferSearch.parameter,
            userMsg:transferSearch.userMsg
          })
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            if(r.rows.length < pageSize ){
              hasNextPage = false;
            }
            transferData[pageSize] = {
              hasNextPage : hasNextPage,
              nextPage : 2,
              data : r.rows
            };
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('TransferList.Updated');
        })
      },
      getResult:function(){
        if(transferData[pageSize]===undefined){
          return false;
        }
        return transferData[pageSize].data;
      },
      getMoreTransferData : function(transferSearch){
        if(transferData[pageSize] === undefined){
          return false;
        }
        var hasNextPage = transferData[pageSize].hasNextPage;
        var nextPage = transferData[pageSize].nextPage;
        var moreTransferData = transferData[pageSize].data;
        resource.query({
          transferSearch:JSON.stringify({
            pageSize:'10',
            pageNo:nextPage.toString(),
            parameter:transferSearch.parameter,
            userMsg:transferSearch.userMsg
          })
        },function(r){
          console.log(r);
          nextPage++;
          if(r.rows.length < pageSize ){
            hasNextPage = false;
          }
          moreTransferData = moreTransferData.concat(r.rows);

          transferData[pageSize] = {
            hasNextPage : hasNextPage,
            nextPage : nextPage,
            data : moreTransferData
          };
          $rootScope.$broadcast('TransferList.Updated');
        })
      },
      hasNextPage: function() {
        if (transferData[pageSize] === undefined) {
          return false;
        }
        return transferData[pageSize].hasNextPage;
      }
    }
  })
  .factory('TransferDetail', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.getTransferDetail,
      transferWaybillData={},pageSize=10;

    var resource = $resource(ApiUrl+':transferDetailSearch',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getTransferDetail : function(transferDetailSearch){
        var hasNextPage = true;
        resource.query({
          transferDetailSearch:JSON.stringify({
            pageSize:'10',
            pageNo:'1',
            transferId:transferDetailSearch.transferId,
            userMsg:transferDetailSearch.userMsg
          })
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            if(r.rows.length < pageSize ){
              hasNextPage = false;
            }
            transferWaybillData[pageSize] = {
              hasNextPage : hasNextPage,
              nextPage : 2,
              data : r.rows
            };
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('TransferDetail.Updated');
        })
      },
      getResult:function(){
        if(transferWaybillData[pageSize]===undefined){
          return false;
        }
        return transferWaybillData[pageSize].data;
      },
      getMoreTransferDetail : function(transferDetailSearch){
        if(transferWaybillData[pageSize] === undefined){
          return false;
        }
        var hasNextPage = transferWaybillData[pageSize].hasNextPage;
        var nextPage = transferWaybillData[pageSize].nextPage;
        var moreTransferDetail = transferWaybillData[pageSize].data;
        resource.query({
          transferDetailSearch:JSON.stringify({
            pageSize:'10',
            pageNo:nextPage.toString(),
            transferId:transferDetailSearch.transferId,
            userMsg:transferDetailSearch.userMsg
          })
        },function(r){
          console.log(r);
          nextPage++;
          if(r.rows.length < pageSize ){
            hasNextPage = false;
          }
          moreTransferDetail = moreTransferDetail.concat(r.rows);

          transferWaybillData[pageSize] = {
            hasNextPage : hasNextPage,
            nextPage : nextPage,
            data : moreTransferDetail
          };
          $rootScope.$broadcast('TransferDetail.Updated');
        })
      },
      hasNextPage: function() {
        if (transferWaybillData[pageSize] === undefined) {
          return false;
        }
        return transferWaybillData[pageSize].hasNextPage;
      }
    }
  })
  .factory('WayBillsToTrans', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.getWayBillsToTrans,
      wbToTransData={},pageSize=10;

    var resource = $resource(ApiUrl+':wbToTransSearch',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getWbToTransData : function(wbToTransSearch){
        var hasNextPage = true;
        resource.query({
          wbToTransSearch:JSON.stringify({
            pageSize:'10',
            pageNo:'1',
            parameter:wbToTransSearch.parameter,
            userMsg:wbToTransSearch.userMsg
          })
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            if(r.rows.length < pageSize ){
              hasNextPage = false;
            }
            wbToTransData[pageSize] = {
              hasNextPage : hasNextPage,
              nextPage : 2,
              data : r.rows
            };
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('WbToTrans.Updated');
        })
      },
      getResult:function(){
        if(wbToTransData[pageSize]===undefined){
          return false;
        }
        return wbToTransData[pageSize].data;
      },
      getMoreWbToTransData : function(wbToTransSearch){
        if(wbToTransData[pageSize] === undefined){
          return false;
        }
        var hasNextPage = wbToTransData[pageSize].hasNextPage;
        var nextPage = wbToTransData[pageSize].nextPage;
        var moreWbToTransData = wbToTransData[pageSize].data;
        resource.query({
          wbToTransSearch:JSON.stringify({
            pageSize:'10',
            pageNo:nextPage.toString(),
            parameter:wbToTransSearch.parameter,
            userMsg:wbToTransSearch.userMsg
          })
        },function(r){
          console.log(r);
          nextPage++;
          if(r.rows.length < pageSize ){
            hasNextPage = false;
          }
          moreWbToTransData = moreWbToTransData.concat(r.rows);

          wbToTransData[pageSize] = {
            hasNextPage : hasNextPage,
            nextPage : nextPage,
            data : moreWbToTransData
          };
          $rootScope.$broadcast('WbToTrans.Updated');
        })
      },
      hasNextPage: function() {
        if (wbToTransData[pageSize] === undefined) {
          return false;
        }
        return wbToTransData[pageSize].hasNextPage;
      }
    }
  })
  .factory('CooperationCompany', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.getCooperationCompany,
      result;

    var resource = $resource(ApiUrl+':searchCompany',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getCompany : function(searchCompany){
        return resource.query({
          searchCompany:searchCompany
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('Company.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('CooperationSite', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.getCooperationCompany,
      result;

    var resource = $resource(ApiUrl+':searchSite',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getSite : function(searchSite){
        console.log(searchSite);
        return resource.query({
          searchSite:searchSite
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('Site.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('SaveTransfer', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.saveTransferOrder,
      result;

    var resource = $resource(ApiUrl+':transferCompanyMsg',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getSaveTransfer : function(transferCompanyMsg){
        return resource.query({
          transferCompanyMsg:transferCompanyMsg
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('SaveTransfer.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('Vehicles', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.getVehicles,
      vehiclesData={},pageSize=10;

    var resource = $resource(ApiUrl+':vehiclesSearch',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getVehicles : function(vehiclesSearch){
        var hasNextPage = true;
        resource.query({
          vehiclesSearch:JSON.stringify({
            pageSize:'10',
            pageNo:'1',
            VEHICLE_NO:vehiclesSearch.VEHICLE_NO,
            VEHICLE_STATUS:'0',
            userMsg:vehiclesSearch.userMsg
          })
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            if(r.rows.length < pageSize ){
              hasNextPage = false;
            }
            vehiclesData[pageSize] = {
              hasNextPage : hasNextPage,
              nextPage : 2,
              data : r.rows
            };
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('Vehicles.Updated');
        })
      },
      getResult:function(){
        if(vehiclesData[pageSize]===undefined){
          return false;
        }
        return vehiclesData[pageSize].data;
      },
      getMoreVehicles : function(vehiclesSearch){
        if(vehiclesData[pageSize] === undefined){
          return false;
        }
        var hasNextPage = vehiclesData[pageSize].hasNextPage;
        var nextPage = vehiclesData[pageSize].nextPage;
        var moreVehiclesData = vehiclesData[pageSize].data;
        resource.query({
          vehiclesSearch:JSON.stringify({
            pageSize:'10',
            pageNo:nextPage.toString(),
            VEHICLE_NO:vehiclesSearch.VEHICLE_NO,
            VEHICLE_STATUS:'0',
            userMsg:vehiclesSearch.userMsg
          })
        },function(r){
          console.log(r);
          nextPage++;
          if(r.rows.length < pageSize ){
            hasNextPage = false;
          }
          moreVehiclesData = moreVehiclesData.concat(r.rows);

          vehiclesData[pageSize] = {
            hasNextPage : hasNextPage,
            nextPage : nextPage,
            data : moreVehiclesData
          };
          $rootScope.$broadcast('Vehicles.Updated');
        })
      },
      hasNextPage: function() {
        if (vehiclesData[pageSize] === undefined) {
          return false;
        }
        return vehiclesData[pageSize].hasNextPage;
      }
    }
  })
